package com.fingard.dsp.bank.directbank.fc11;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

import com.fingard.dsp.bank.directbank.DirectBase;

public class FC11Base extends DirectBase {
	protected String getCharset() {
		return getBankConfig().getCharset("UTF-8");
	}
	
	protected String getCharset(String p_def, String p_for) {
		return getBankConfig().getCharset(p_def, p_for);
	}
	
    public static String generateAES(String sSrc, String sKey) throws Exception {
        if (sKey.length() > 16) {
        	sKey = sKey.substring(sKey.length() - 16, sKey.length());
        }
        byte[] raw = sKey.getBytes("ASCII");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes());
        return Base64.encodeBase64String(encrypted);
    }
}
